<?php
include("../lib/db.lib");
include("../lib/std.lib");



//Create related ones

$p_id = $_GET["id"];
$p_id0 = $_GET["id"];

$url="Topics/".$p_id;
$url = preg_replace("/\/\//","/",$url);

$cat_chain=preg_split("/\//",$url);
$num=count($cat_chain);
$cat_chain[$num-1] = preg_replace('/([0-9]*).txt/',"",$cat_chain[$num-1]);
$related_c_id=cat_check($cat_chain,1);




if($related_c_id ){

$shop_id=3;
$list=cat_all_pd_list($shop_id,$related_c_id,0);

$i=0;

$r_str="<table style=\"float:right;margin-right:5px;\" width=\"20%\">";
$r_str.= "<tr valign=\"top\" bgcolor=\"#D3BADA\">\n";
$r_str.= "<td colspan=2>&nbsp;Related ones:</td >";
$r_str.= "</tr>\n";

foreach($list as $key=>$val){

	$i++;
	if($i>10){ $unfinished=1; break;}

	$local_p_id = $val['id'];
	if( preg_match('/http/',$local_p_id)){
		$article_url = $local_p_id;
		//if($base_url!="http://www.xys.org/") $base_url="http://xys.cybersome.com/";
		$article_url = preg_replace("/http:\/\/www\.xys\.org\//",${base_url},$article_url);
	} else{
		$article_url = "v.php?id=$local_p_id";
	}


	if($i % 2){
                $tr_color= "<tr valign=\"top\" bgcolor=\"#F1FFE9\">\n";
        }else{
                $tr_color= "<tr valign=\"top\" bgcolor=\"#F1FFCA\">\n";
        }

    
	$r_str.= $tr_color;
	$r_str.= "<td width=\"15\" align=right> $i. </td>\n<td >";
	$r_str.= "<a target=_parent href='${article_url}'>" . $val['title']. "</a> <span class='author'>( <a target=_parent href=\"p_search.php?key_au=" .$val['author']."\" >".$val['author']."</a>".$val['date'].")</span></td>\n";
	

	$r_str.= "</tr>\n";
}





if(! $i) {
 $r_str.= "<tr valign=\"top\" >\n";
 $r_str.= "<td colspan=2>&nbsp; No related. </td >";
 $r_str.= "</tr>\n </table>\n";
}
if( $unfinished) {
 $r_str.= "<tr valign=\"top\" bgcolor=\"#D3BADA\">\n";
 $r_str.= "<td colspan=2>&nbsp; <a target=_parent href=\"p_cat_pd_list.php?cat_id=$related_c_id\"> More ... </a></td >";
 $r_str.= "</tr>\n </table>\n";
}
else
{
 $r_str.= "<tr valign=\"top\" bgcolor=\"#D3BADA\">\n";
 $r_str.= "<td colspan=2>&nbsp;</td >";
 $r_str.= "</tr>\n </table>\n";
}

}


/// -----------------------------  end of "related ones"  -------------------

$str = "";

$file_id0 = "/".$_GET["id"];
$file_id0 = preg_replace("/\/\//","/",$file_id0);

$file_id = preg_replace("/\//","_",$file_id0);

if(file_exists("./cache/$file_id"))
{
	$fh=fopen("./cache/$file_id","r");
	while(!feof($fh)) $str.=fread($fh, 200000);
	fclose($fh);
}
else
{
	$fh=fopen("http://www.xys.org/xys/$file_id0","r");
	while(!feof($fh)) $str.=fread($fh, 200000);
	fclose($fh);

	//$fh=fopen("./cache/$file_id","w");
	//fwrite($fh, $str);
	//fclose($fh);

}
//$str = preg_replace('/<[^>]*>/',"",$str);

$str = preg_replace('/</',"&lt;",$str);
$str = preg_replace('/>/',"&gt;",$str);

if( preg_match('/zhaojianyi/',$local_p_id)){
	$str = "";
}

// remove xys marks
$str = preg_replace('/www\.xys\./',"xys.",$str);
$str = preg_replace('/www\.xysforum\./',"xysforum.",$str);



//----- multi page start ------

$page_no=param("page");
$page_length=3000;
if(! $page_no>0){ $page_no = 1; }

mb_internal_encoding("GB2312");
$total_pages=ceil(mb_strlen($str)/$page_length);

if($page_length*$page_no<mb_strlen($str))
$page_next="<a target=_parent href=\"v.php?id=$p_id&page=".($page_no+1)."\">-&gt;&gt;&gt;&gt;&gt;</a>\n";
if($page_length*$page_no<mb_strlen($str))
$page_next_2="<a target=_parent href=\"v.php?id=$p_id&page=".($page_no+1)."\">[ Next Page ]</a>\n";

if( $page_no>1 ){ $page_last="<a target=_parent href=\"v.php?id=$p_id&page=".($page_no-1)."\">&lt;&lt;&lt;&lt;</a>\n"; }
if( $page_no>1 ){ $page_first="[ <a target=_parent href=\"v.php?id=$p_id\">First Page </a> ]"; }

if($page_no>1){
$str_pre = mb_substr($str, ($page_no-1)*$page_length-$page_length/10,$page_length/10);
//$str_pre =preg_replace("/(.*)\n([^\n]+)$/","$2",$str_pre);
preg_match("/([^\n]+)$/",$str_pre,$matched);
$str_pre = $matched[1];
}

$str = mb_substr($str, ($page_no-1)*$page_length,$page_length);
$str =preg_replace("/\n(.*)$/","",$str);

$str ="$page_last $str_pre$str";
$str .="$page_next\n";
//$str.="<hr width=500>";
if($total_pages>1) $str .="$page_first  Page $page_no of total $total_pages $page_next_2 ";


//parsing url and image src
$str = preg_replace( '/\[img\]http:\/\/([^\s<\n]+)\[\/img\]/i' ," <img src=\"http://www.xys-reader.org/xys/d_fetch_img.php?img=$1\" width=\"450\">", $str);


$str=preg_replace_callback( '/([^>\"\'=]{1}|pre>|p>|br>)http:\/\/([0-9a-zA-Z;,%\?\&\/:\.\-_=#~]*)/i',
                create_function(
                    '$matches',
                    'if(preg_match("/jpg$/i",$matches[2])){ 
			return "$matches[1]<img src=\"http://www.xys-reader.org/d_fetch_img.php?img=$matches[2]\" width=450>";
	             }else{ if(strlen($matches[2])>54) $ext=" ..."; return "$matches[1]<a href=\"http://$matches[2]\">http://".substr($matches[2],0,54)."${ext}</a>";}'
                ),
                $str );


//$str = preg_replace( '/[^\"\'=>]http:\/\/([^\s<\n]+)/i' ," <a href=\"http://$1\">http://$1</a>", $str);

//----- multi page end ------


$str .= "\n\nLink: <font size=2> ${base_url}xys$file_id0 </font>";


$str = "<pre style=\"float:left;\">\n$str\n</pre>$r_str \n
<div style=\"clear:both;\"></div>";

/*
$fh=fopen("log","w");
fwrite($fh, $str );
fclose($fh);
*/


if($_GET["no_encode"] ){
	$str = iconv("GBK","UTF-8",$str);
	//$str=mb_convert_encoding($str,"UTF-8","GBK");
	$str = base64_encode($str);
	echo $str;
	exit(0);

}

//$str=gb2utf8($str);
$str=mb_convert_encoding($str,"UTF-8","GBK");
//$str = iconv("GBK","UTF-8",$str);
$str = base64_encode($str);
/*
echo $str;
exit(0);
*/

/// Code stops here.


$pass=65;
$newstr = chr($pass);
for($i=0;$i<strlen($str);$i++){
	$newstr.= chr( (ord(substr($str,$i,1)) + ($i+1)*($i+1) - 43 +$pass) % 82+43);
	//$newstr.= chr( ((ord(substr($str,$i,1))  - 43 +$pass) % 82) +43);
}
echo $newstr;

?>
